package com.xujiakai.car.maintain.domain.entity;

import com.xujiakai.car.maintain.application.service.impl.SystemManageService;
import com.xujiakai.car.maintain.domain.dto.AccountDTO;
import com.xujiakai.car.maintain.domain.dto.UserrightsDTO;
import com.xujiakai.car.maintain.infrastructure.mapper.DaoFactory;
import com.xujiakai.car.maintain.infrastructure.utils.StringUtils;
import com.xujiakai.car.maintain.userinterface.views.UserView;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class Userinfo {
    private Integer id;

    private String username;

    private String jobnumber;

    private String concatinfo;

    private String address;

    private Date entrytime;

    private String userflag;

    private Integer sectorid;

    @Resource
    private DaoFactory daoFactory;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getJobnumber() {
        return jobnumber;
    }

    public void setJobnumber(String jobnumber) {
        this.jobnumber = jobnumber == null ? null : jobnumber.trim();
    }

    public String getConcatinfo() {
        return concatinfo;
    }

    public void setConcatinfo(String concatinfo) {
        this.concatinfo = concatinfo == null ? null : concatinfo.trim();
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address == null ? null : address.trim();
    }

    public Date getEntrytime() {
        return entrytime;
    }

    public void setEntrytime(Date entrytime) {
        this.entrytime = entrytime;
    }

    public String getUserflag() {
        return userflag;
    }

    public void setUserflag(String userflag) {
        this.userflag = userflag == null ? null : userflag.trim();
    }

    public Integer getSectorid() {
        return sectorid;
    }

    public void setSectorid(Integer sectorid) {
        this.sectorid = sectorid;
    }


    public boolean modifyUser(UserView userView, int[] privileges, String type) {
        boolean flag = false;
        SystemManageService.logger.info("获取到的userview.userinfoid:" + userView.getUserinfoid());
        try {
            if ("c".equals(type)) {// 创建
                // 1.添加用户信息
                com.xujiakai.car.maintain.domain.dto.UserinfoDTO userinfo = new com.xujiakai.car.maintain.domain.dto.UserinfoDTO();
                userinfo.setUsername(userView.getUsername());
                userinfo.setJobnumber(userView.getJobnumber());
                userinfo.setConcatinfo(userView.getConcatinfo());
                userinfo.setAddress(userView.getAddress());
                userinfo.setEntrytime(StringUtils.getDateFromStr(userView.getEntrytime()));
                userinfo.setUserflag("1");
                userinfo.setSectorid(StringUtils.getIntegerValue(userView.getSectorid(), -1));
                daoFactory.getUserinfoMapper().insertSelective(userinfo);
                // 2.添加账户信息
                AccountDTO account = new AccountDTO();
                account.setPasswords("000000");// 初始密码六个0
                account.setAccountnumber(userView.getAccountnumber());
                account.setAccountflag("1");
                SystemManageService.logger.info("====================================>用户信息表的编号:" + userinfo.getId());
                account.setUserinfoid(userinfo.getId());
                daoFactory.getAccontMapper().insertSelective(account);
                // 3.添加权限
                if (privileges != null) {
                    for (int i = 0; i < privileges.length; i++) {
                        // 新建用户权限实体
                        UserrightsDTO userrights = new UserrightsDTO();
                        userrights.setAccountid(account.getId());
                        userrights.setPermissionid(privileges[i]);
                        daoFactory.getUserrightsMapper().insertSelective(userrights);
                    }
                }
                flag = true;
            } else if ("u".equals(type)) {// 修改
                // 用户信息
                com.xujiakai.car.maintain.domain.dto.UserinfoDTO userinfo = new com.xujiakai.car.maintain.domain.dto.UserinfoDTO();
                userinfo.setId(StringUtils.getIntegerValue(userView.getUserinfoid(), -1));
                userinfo.setUsername(userView.getUsername());
                userinfo.setJobnumber(userView.getJobnumber());
                userinfo.setConcatinfo(userView.getConcatinfo());
                userinfo.setAddress(userView.getAddress());
                userinfo.setEntrytime(StringUtils.getDateFromStr(userView.getEntrytime()));
                userinfo.setUserflag(userView.getUserflag());
                userinfo.setSectorid(StringUtils.getIntegerValue(userView.getSectorid(), -1));
                flag = daoFactory.getUserinfoMapper().updateByPrimaryKey(userinfo) > 0;

                SystemManageService.logger.info("===================================>修改用户信息:" + flag);

                // 账号信息
                AccountDTO account = new AccountDTO();
                account.setId(Integer.parseInt(userView.getAccountid()));
                account.setAccountnumber(userView.getAccountnumber());
                account.setUserinfoid(userinfo.getId());
                flag = daoFactory.getAccontMapper().updateByPrimaryKeySelective(account) > 0;


                SystemManageService.logger.info("===================================>修改账户信息:" + flag);

                // 更新权限信息
                flag = daoFactory.getUserinfoMapper().delUserRights(account.getId() + "") > 0;

                SystemManageService.logger.info("===================================>删除所有权限信息:" + flag);

                // 添加权限信息
                if (privileges != null) {
                    for (int i = 0; i < privileges.length; i++) {
                        UserrightsDTO userrights = new UserrightsDTO();
                        userrights.setAccountid(account.getId());
                        userrights.setPermissionid(privileges[i]);
                        flag = daoFactory.getUserrightsMapper().insertSelective(userrights) > 0;
                        SystemManageService.logger.info("===================================>修改账户信息:" + flag);
                    }
                }
                flag = true;
            } else if ("d".equals(type)) {// 删除
                // 跟新用户标记
                daoFactory.getUserinfoMapper().modifyUser(userView.getUserinfoid(), userView.getUserflag());
                SystemManageService.logger.info("============================>删除用户成功!,userinfoid={}", userView.getUserinfoid());
                flag = true;
            }
        } catch (Exception e) {
            SystemManageService.logger.error("====>用户信息操作有误!");
            flag = false;
        }
        return flag;
    }
}